import 'package:carousel_slider/carousel_slider.dart';
import 'package:development_assistant/config/font.dart';
import 'package:development_assistant/config/fonticon.dart';
import 'package:development_assistant/controller/docker_controller.dart';
import 'package:development_assistant/controller/server_controller.dart';
import 'package:development_assistant/ui/screen/widget/custom_text.dart';
import 'package:development_assistant/ui/screen/widget/frosted_glass.dart';
import 'package:development_assistant/ui/screen/widget/home_background.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:google_fonts/google_fonts.dart';

import '../../../config/colors.dart';
import '../../../config/image_config.dart';
class ServerAssistant extends StatelessWidget {
  ServerAssistant({super.key});

  final ServerController serverController=Get.put(ServerController());
  final DockerController dockerController=Get.put(DockerController());
  @override
  Widget build(BuildContext context) {
    return HomeBackground(
        front: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 35.h,horizontal: 20.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _changeGlobal(),
                _server(),
                SizedBox(height: 10.h,),
                _envConfig(),
                SizedBox(height: 10.h,),
                _docker()
              ],
            ),
          )
        )
    );
  }
  _changeGlobal(){

    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        IconButton(
            onPressed: (){
              Get.arguments["selectIndex"]=0;
              if(serverController.isChinese.value==0){
                Get.updateLocale(const Locale("cn",'EN'));
                serverController.isChinese.value=1;
              }else{
                Get.updateLocale(const Locale("cn",'CN'));
                serverController.isChinese.value=0;
              }
              Get.snackbar("修改语言".tr, serverController.isChinese.value==0?"成功修改语音为:中文".tr:"成功修改语音为:英文".tr);
            },
            icon: Icon(FontIcons.translation)
        ),
        IconButton(
            onPressed: (){
              Get.arguments["selectIndex"]=0;
              Get.changeTheme(Get.isDarkMode?ThemeData.light():ThemeData.dark());
              Future.delayed(const Duration(milliseconds: 200), () {
                Get.forceAppUpdate();
                Get.snackbar("主题颜色".tr, Get.isDarkMode?"成功修改主题为:暗黑模式".tr:"成功修改主题为:明亮模式".tr);
              });
            },
            icon: Icon(Get.isDarkMode?Icons.sunny_snowing:Icons.nights_stay)
        )
      ],
    );
  }

  _server(){
    return FutureBuilder(
      future: serverController.getServerList(),
      builder: (context,snapshot) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            //添加服务器
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("服务器".tr,style: FontConfig.titleStyle,),
                SizedBox(
                  child: ElevatedButton.icon(
                    onPressed: (){
                      serverController.linkStatue.value=0;
                      serverController.linkResult.value="";
                      Get.toNamed("/AddServer");
                    },
                    icon: const Icon(Icons.add),
                    label: Text("新建".tr),
                    style: ElevatedButton.styleFrom(
                        backgroundColor: ColorsConfig().buttonColorOfTheme,
                        foregroundColor: Colors.white,
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(10)
                        )
                    ),
                  ),
                ),
              ],
            ),
            //服务器列表数据项
            Obx(() =>serverController.serverList.isEmpty?_serverFigure() :SizedBox(
              height: 160.h,
              child: CarouselSlider(
                options: CarouselOptions(
                    height: 400.0,
                    aspectRatio: 16/9,
                    initialPage: 0,
                    enlargeCenterPage: true,
                    onPageChanged: (index,reason){

                    }
                ),
                items: List<Widget>.generate(serverController.serverList.length, (index) {
                  return Container(
                      width: 400.w,
                      margin: EdgeInsets.symmetric(vertical: 10.h),
                      padding: EdgeInsets.symmetric(vertical: 10.h,horizontal: 20.w),
                      decoration: BoxDecoration(
                          gradient: ColorsConfig().cardColorOfTheme,
                          borderRadius: BorderRadius.circular(20)
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              CustomText(text: serverController.serverList[index].serverType, textStyle: FontConfig.databaseNameStyle),
                              IconButton(
                                splashColor: Colors.transparent,
                                highlightColor: Colors.transparent,
                                onPressed: (){
                                  serverController.getServerInfo(serverController.serverList[index].serverID);
                                  serverController.linkStatue.value=0;
                                  serverController.linkResult.value="";
                                  Get.toNamed("/EditServer");
                                },
                                icon: const Icon(Icons.more_horiz),
                              ),
                            ],
                          ),
                          CustomText(text: "${"服务器名称".tr}: ${serverController.serverList[index].serverName}", textStyle:FontConfig.databaseInformationStyle),
                          CustomText(text: "${"主机号".tr} : ${serverController.serverList[index].serverHost}", textStyle:FontConfig.databaseInformationStyle),
                          CustomText(text: "${"端口号".tr} : ${serverController.serverList[index].serverPort}", textStyle:FontConfig.databaseInformationStyle),
                          CustomText(text: "${"Url地址".tr} : ${serverController.serverList[index].serverUrl}", textStyle:FontConfig.databaseInformationStyle)
                        ],
                      )
                  );
                }),
              ),
            ))
          ],
        );
      }
    );
  }

  _envConfig(){
    envButton(String envType){
      return Container(
        width: 150.w,
        height: 50.h,
        padding: EdgeInsets.symmetric(horizontal: 10.h),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          gradient: ColorsConfig().cardColorOfTheme
        ),
        child: Row(
          children: [
            FrostedGlass(
                width: 20.h,
                height: 20.h,
                color: Colors.white12,
                borderRadius: BorderRadius.circular(50),
                sigmaX: 10, sigmaY:10,
                child: Text(
                  envType[0],
                  style:TextStyle(color: ColorsConfig.envTagColors[envType],fontWeight: FontWeight.bold,fontSize: 15.sp),)
            ),
            SizedBox(width: 5.w,),
            IconButton(
              highlightColor: Colors.transparent,
              onPressed: (){
                Get.defaultDialog(
                  title: envType,
                  titleStyle: FontConfig.titleStyle,
                  content: FutureBuilder(
                    future:serverController.getEnvInfo(envType),
                    builder: (context,snapshot) {
                      return Container(
                        margin: EdgeInsets.symmetric(horizontal: 10.w),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text("前置Url".tr,style: FontConfig.fieldTitle,),
                            SizedBox(height: 5.h,),
                            TextField(
                              controller: TextEditingController(text: serverController.envUrl.value),
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText:"默认为空".tr,
                                  border: OutlineInputBorder(
                                    borderRadius: BorderRadius.circular(15),
                                  )
                              ),
                              onChanged: (e){
                                serverController.envName.value=envType;
                                serverController.envUrl.value=e;
                              },
                            ),
                          ],
                        ),
                      );
                    }
                  ),
                  confirm: TextButton(
                      style: TextButton.styleFrom(
                        backgroundColor:ColorsConfig().buttonColorOfTheme,
                        foregroundColor: Colors.white,
                      ),
                      onPressed: (){
                        serverController.setEnv();
                      },
                      child: Text("确定".tr)
                  ),
                  cancel: TextButton(
                      style: TextButton.styleFrom(
                        backgroundColor: ColorsConfig().buttonColorOfTheme,
                        foregroundColor: Colors.white,
                      ),
                      onPressed: (){
                        Get.back();
                      },
                      child: Text("取消".tr)
                  )
                );
              },
              icon: Text(envType,style: GoogleFonts.lato(
                  textStyle: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 15.sp
                  )
              ),),
            )
          ],
        ),
      );
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("环境配置".tr,style: FontConfig.titleStyle,),
        SizedBox(height: 5.h,),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            envButton("测试环境".tr),
            envButton("正式环境".tr),
          ],
        ),
        SizedBox(height: 10.h,),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            envButton("体验环境".tr),
            envButton("云端环境".tr),
          ],
        )
      ],
    );
  }

  _docker(){
    return FutureBuilder(
      future: dockerController.getDockerList(),
      builder: (context,snapshot) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text("Docker镜像".tr,style: FontConfig.titleStyle,),
                ElevatedButton.icon(
                  onPressed: (){
                    dockerController.getDockerList();
                    Get.toNamed("/AddDocker");
                  },
                  icon: const Icon(Icons.create_new_folder_outlined),
                  label: Text("创建".tr),
                  style: ElevatedButton.styleFrom(
                      backgroundColor: ColorsConfig().buttonColorOfTheme,
                      foregroundColor: Colors.white,
                      shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10)
                      )
                  ),
                ),
              ],
            ),
            Obx(() => dockerController.dockerList.isEmpty?_dockerFigure():Wrap(
              children: List<Widget>.generate(dockerController.dockerList.length, (index) {
                return Container(
                    width: 300.w,
                    height: 80.h,
                    margin: EdgeInsets.symmetric(vertical: 10.h),
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    decoration: BoxDecoration(
                        gradient: ColorsConfig().cardColorOfTheme,
                        borderRadius: BorderRadius.circular(20)
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            CustomText(text: dockerController.dockerList[index].dockerName, textStyle: FontConfig.databaseNameStyle),
                            IconButton(
                                splashColor: Colors.transparent,
                                highlightColor: Colors.transparent,
                                onPressed: () {
                                  dockerController.getDockerInfo(dockerController.dockerList[index].dockerID);
                                  Get.toNamed("/EditDocker");
                                },
                                icon: const Icon(Icons.more_horiz)
                            ),
                          ],
                        ),
                        CustomText(text: "${'镜像ID'.tr} : ${dockerController.dockerList[index].dockerCid}", textStyle: FontConfig.databaseInformationStyle),
                        CustomText(text: "${'镜像标签'.tr}: ${dockerController.dockerList[index].dockerTag}", textStyle: FontConfig.databaseInformationStyle),
                      ],
                    )
                );
              }),
            )),
          ],
        );
      }
    );
  }

  _serverFigure(){
    return Container(
      height: 150.h,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(15),
        gradient:ColorsConfig().figureOfTheme
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            ImageConfig.figureImage['server']!,
            width: 180.w,
          ),
          Container(
            width: 130.w,
            child: Column(
              children: [
                SizedBox(height: 55.h,),
                Text("管理您的服务器".tr,style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 15.sp),),
                SizedBox(height: 5.h,),
                Text("更好的帮你记住服务器信息".tr,style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 10.sp),),
              ],
            ),
          )
        ],
      ),
    );
  }

  _dockerFigure(){
    return Container(
      height: 150.h,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(15),
          gradient: ColorsConfig().figureOfTheme
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            ImageConfig.figureImage['docker']!,
            width: 180.w,
          ),
          Container(
            width: 130.w,
            height: 200.h,
            padding: EdgeInsets.symmetric(vertical: 50.h),
            child: Column(
              children: [
                Text("Docker镜像".tr,
                  style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 15.sp),),
                SizedBox(height: 5.h,),
                Text("Docker构建信息不再丢失".tr,
                  style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 10.sp),),
              ],
            ),
          )
        ],
      ),
    );
  }
  
}
